#include "stdafx.h"
#include "ckSchema.h"
#include "ckGroup.h"
#include "ckEnumGroup.h"

namespace ck
{
	namespace data
	{
		Schema::Schema()
		{
		}


		Schema::~Schema()
		{
			Clear();
		}

		void Schema::Clear()
		{
			Group* pGroup = NULL;
			std::vector<Group*>::iterator iter;
			for (iter = m_groups.begin(); iter != m_groups.end(); iter++)
			{
				pGroup = *iter;
				pGroup->Release();
			}
			m_groups.clear();
		}

		RESULTCODE Schema::AddGroup(Group* pGroup)
		{
			if (!pGroup)
			{
				return RC_FAILURE;
			}

			m_groups.push_back(pGroup);

			return RC_SUCCESS;
		}

		Group* Schema::GetGroup(const char* szName)
		{
			if (szName == NULL)
			{
				return NULL;
			}

			Group* pGroup = NULL;
			std::vector<Group*>::iterator iter;
			for (iter = m_groups.begin(); iter != m_groups.end(); iter++)
			{
				pGroup = *iter;
				if (pGroup)
				{
					if (strcmp(pGroup->GetName(), szName) == 0)
					{
						return pGroup;
					}
				}
			}
			return NULL;
		}

		EnumGroup* Schema::GetGroups()
		{
			EnumGroup* pGroups = new EnumGroup();

			Group* pGroup = NULL;
			std::vector<Group*>::iterator iter;
			for (iter = m_groups.begin(); iter != m_groups.end(); iter++)
			{
				pGroup = *iter;
				pGroup->AddRef();
				pGroups->Add(pGroup);
			}
			return pGroups;
		}

	}
}
